package com.fwmagic.spark.core.cases.groupwithtopn

import scala.collection.mutable.ArrayBuffer

//class OrderingBean(val subject: String, val teacher: String, val count: Integer) extends Ordered[OrderingBean] with Serializable {
class OrderingBean(val subject: String, val teacher: String, val count: Int) extends Ordered[OrderingBean] with Serializable {

    val buffer = new ArrayBuffer[(String, String, Int)]()

    buffer += ((subject, teacher, count))

    /**
      * 对象次数相等
      *
      * @return
      */
    override def compare(that: OrderingBean): Int = {
        if (this.count == that.count) {
            buffer += ((that.subject, that.teacher, that.count))
            0
        } else {
            -(this.count - that.count)
        }
    }

    override def toString = {
        if (buffer.size > 1) {
            buffer.toString()
        } else {
            s"OrderingBean($subject, $teacher, $count)"
        }
    }
}
